<?php

namespace App\Http\Controllers\Biz;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

use App\Repositories\Eloquent\Mgt\UserRepository as User;
use App\Repositories\Eloquent\Mgt\BookRepository as Book;
use App\Repositories\Eloquent\Mgt\ActivityRepository as Activity;
use App\Repositories\Eloquent\Biz\BorrowRepository as Borrow;
use App;

class BankController extends Controller
{
    protected $user, $book, $activity, $borrow;

    public function __construct(User $user, Book $book, Activity $activity, Borrow $borrow)
    {
        parent::__construct();

        $this->user = $user;
        $this->book = $book;
        $this->activity = $activity;
        $this->borrow = $borrow;
    }

    public function sign()
    {
        $enabled = \App\Models\Mgt\Activity::enabled();

        return view($this->view, ['enabled'=>$enabled]);
    }


    public function show(Request $request)
    {
        $users = $books = $borrows = array();

        $query = $request->input('query');

        $account = $request->input('account');

        $isbn = $request->input('isbn');

        if (isset($account))
            $users = $this->user->index($account)->get();

        if (isset($query)) {

            //2020.3.24修改strlen($query)>6,因有ISBN不为标准的13位
            if (preg_match('/^\d+$/i', $query) && strlen($query) > 6) {

                $isbn = $query;

                $map = $this->book->index($isbn)->pluck('id');

                if (!empty($map))
                    $borrows = $this->borrow->wheres($map)->get();

                $books = $this->book->index($query)->get();

            } elseif (preg_match('/^\d+$/i', $query)) {

                $account = $query;

                $users = $this->user->index($account)->get();

            } else {

                $user = $this->user->index($query)->get();
            }
        }

        if(!empty($users))
            $users = $this->user->transforms($this->act, $users);

        return view($this->view, [
                    'activity'=>$this->act,
                    'users'=>$users,
                    'account'=>$account,
                    'books'=>$books,
                    'borrows'=>$borrows,
                    'isbn'=>$isbn,
                    'query'=>$query,
                ]);
    }


    /**
     * [默认用户列表:后台注册/普通读者]
     * @param Request $request [description]
     * @return [type]           [description]
     */
    public function index(Request $request)
    {
        $query = $request->query('query');

        $users = $this->user->index($query, USER_TYPE_READER);

        $activity = $this->act;

        $paging = round($users->count()/2);

        return view($this->view, compact('users', 'activity','paging'));
    }

    /**
     * [网络注册/普通读者]
     * @param Request $request [description]
     * @return [type]           [description]
     */
    public function apply(Request $request)
    {
        $users = $this->user->index($request->query('query'), USER_TYPE_READER, REGISTER_NETWORK);

        $activity = \App\Models\Mgt\Activity::current();

        return view($this->view, compact('users', 'activity'));
    }



    /**
     * 签到
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function checkin(Request $request)
    {
        return $this->user->checkin($request);
    }


    /**
     * 借书
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function borrow(Request $request)
    {
        return $this->user->borrow($request);
    }

    /**
     * 还书
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function return(Request $request)
    {
        $request->activity = $this->act;

        return $this->user->return($request);
    }

    /*public function m(Request $request)
    {
        $summaries = \App\Models\Biz\Summary::all();

        foreach($summaries as $summary){
            $user = \App\Models\Mgt\User::find($summary->uid);
            $count = $user->records->where('type',3)->count();
            if($count>0){
                $summary->awards = $count;
                $summary->update();
            }
        }
    }*/



}
